#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

float complexity(Mat src) {
    Mat outline;
    int ink = countNonZero(src);
    dilate(src, outline, Mat());
    outline = outline - src;
    dilate(outline, outline, Mat());
    int perimeter = countNonZero(outline) / 3;
    return ((float)perimeter*perimeter / ink);
}

int main(int argc, char* argv[]) {
    Mat image;

    image = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
    threshold(image, image, 100, 255, THRESH_BINARY);

    cout << complexity(image) << endl;
}
